//
//  HS_Billboard_ViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HS_Billboard_ViewController.h"
#import "HZJPlayListDetailViewController.h"
#import "HZ_Wab_ViewController.h"
#import "HSAFNetTool.h"
#import "HZ_Billbaord_tablecell.h"
#import "HZ_Billboard_Model.h"
#import "HZ_Billboard_Model.h"
#import "HZ_refsArray.h"
@interface HS_Billboard_ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

#define LISTURL @"?app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.882548834006&longtitude=121.5391476381413"

@implementation HS_Billboard_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.786 green:1.000 blue:0.977 alpha:1.000];
    
    
    
    
    self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    
    [self.table registerClass:[HZ_Billbaord_tablecell class] forCellReuseIdentifier:@"HZ_Billbaord_tablecell"];
    
    self.table.delegate = self;
    self.table.dataSource = self;
    
    
    
    [self.view addSubview:self.table];
    [self HSAFNetTool];
    
    
}


-(void)HSAFNetTool{
    
    
    [HSAFNetTool GETNetWithUrl:@"http://api.songlist.ttpod.com/channels/bhb/children?v=v8.4.1.2016032512" body:nil headerFile:nil response:HSJSON success:^(id result) {
        
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
        
        for (NSDictionary *dic in result) {
            
            HZ_Billboard_Model *billboard = [[HZ_Billboard_Model alloc] initWithDataSource:dic];
            
            
            
            [_dataArray addObject:billboard];
            
            
        }
        
        
        
        NSLog(@"%@", _dataArray);
        
        
        
        
        [_table reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    HZ_Billboard_Model *billboard = _dataArray[section];
//    HZ_refsArray *refs = billboard.refs[section];
//
    return billboard.refs.count;
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HZ_Billbaord_tablecell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZ_Billbaord_tablecell"];
    
    HZ_Billboard_Model *billboard = _dataArray[indexPath.section];
    
    NSLog(@"%@", billboard);
    
    HZ_refsArray *refs = billboard.refs[indexPath.item];
    
    NSLog(@"%@", refs);
    
    cell.model = refs;

    return  cell;

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    
    return 3;
    
    
}
//分区高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
  return   130;
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    HZ_Billboard_Model *billboard = _dataArray[section];
    
    
    return billboard.name;
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HZ_Billboard_Model *billboard = _dataArray[indexPath.section];
    HZ_refsArray *refs = billboard.refs[indexPath.row];
    if (refs.link) {
        HZ_Wab_ViewController *webVC = [[HZ_Wab_ViewController alloc] init];
        webVC.refs = refs;
        [self.navigationController pushViewController:webVC animated:YES];
    } else {
        
        HZJPlayListDetailViewController *listVC = [[HZJPlayListDetailViewController alloc] init];
        listVC.urlString = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@%@", refs.songlist_id, LISTURL];
        listVC.picUrl = refs.images.pic;
        listVC.detailStr = refs.title;
        [self.navigationController pushViewController:listVC animated:YES];
        
    }
    [_table deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
